import http from "../../../utils/http"
import util from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    list:[],
    finish: false,
    loading: false,
    page: 1,
    couponShow: false,
    closeOnClickOverlay: true,
    inputMoney:'',
    optionData: {},
    showDate: false,
    date: '',
    moneyTotal: {},
    minDate: new Date(2024, 6, 1).getTime(),
  },

  onDisplayDate() {
    this.setData({ showDate: true });
  },
  onCloseDate() {
    this.setData({ showDate: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  },
  onConfirmDate(event) {
    const [start, end] = event.detail;
    this.setData({
      showDate: false,
      date: `${this.formatDate(start)} - ${this.formatDate(end)}`,
    });
    this.reload();
  },
  withdrawTap(){
    util.skip('/service/distribution/withdraw/withdraw?money='+this.data.optionData.money)
  },
  setInputMoney(e){
    this.setData({
      inputMoney: e.detail > 1000 ? 1000 : e.detail
    })
  },
  openPopup(){
    this.setData({
      couponShow: true
    })
  },
  onClose(){
    this.setData({
      couponShow:false
    })
  },
  onChange(e){
    this.setData({
      active: e.detail.index
    })
    this.reload()
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    let data = {
      page: this.data.page,
      type: 'all',
      date: this.data.date,
    }
    if(this.data.active === 0){
      data['type'] = 'all'
    }else if(this.data.active === 1){
      data['type'] = 'accounted'
    }else if(this.data.active === 2){
      data['type'] = 'out'
    }
    http.get('/addons/shopro/commission.reward/index', data).then(res => {
      if(res.data.rewards.data.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data.rewards.data)
      this.setData({
        list: arr,
        moneyTotal: {
          total_money: res.data.total_money,
          out_money: res.data.out_money,
        }
      })
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const brokerage = JSON.parse(wx.getStorageSync('brokerage'))
    this.setData({
      active: options.active ?  Number(options.active) : 0,
      optionData: brokerage
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const date = new Date();
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    this.setData({
      date: `${year}/${month}/${day} - ${year}/${month}/${day + 1}`,
    })
    this.reload()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})